import {Component, OnInit, ViewEncapsulation, ChangeDetectionStrategy, ViewChild, OnDestroy} from '@angular/core';
import {CommunityService} from './community.service';
// import {AdvertisementRespVO} from '@interface/banner.model';
import {Subject} from 'rxjs/Subject';
// import {SocialCircle} from '@interface/community.model';
// import {FeedRespVO} from '@interface/interface.model';
import {Observable} from 'rxjs/Observable';
import {SeedInfiniteScroll} from '@shared/components/infinite-scroll/seed-infinite-scroll/seed-infinite-scroll.component';
import {ToolsService} from '../../shared/service/tools/tools.service';
import {AdvertisementRespVO, FeedRespVO, SocialCircle} from "@interface/index";

@Component({
  selector: 'community',
  templateUrl: './community.component.html',
  styleUrls: ['./community.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class CommunityComponent implements OnInit, OnDestroy {
  @ViewChild(SeedInfiniteScroll) infiniteScroll;

  public banners: Array<AdvertisementRespVO>;
  public myCircles: Array<SocialCircle> = [];
  public hotCircles: Array<SocialCircle> = [];
  public primeFeeds: Array<FeedRespVO> = [];
  public stateStore = {
    haveFeeds: true,
    haveMoreFeeds: true,
    canChange: true // 是否可以换一换
  };
  private params = {
    page: 1,
    rows: 10
  };

  public ngUnsubscribe: Subject<void> = new Subject<void>();

  constructor(public tools: ToolsService,
              public communityService: CommunityService) {

  }

  // /**
  //  * @description 获取轮播图
  //  * @author ouyqx-691
  //  * @date 2017-12-11
  //  */
  // getSlider() {
  //   this.communityService.getSlider().takeUntil(this.ngUnsubscribe).subscribe(res => {
  //     if (res.result === '0' && res.data) {
  //       this.banners = res.data.list;
  //     }
  //   });

  // };

  /**
   * @description 前往商品详情
   * @author ouyqx-691
   * @date 2017/11/24
   * @param news 单个轮播对象
   */
  goToBannerTarget(slide) {
    // 外链且存在外链url
    if (slide.linkType === 2 && slide.detailUrl) {
        window.open(slide.detailUrl);
    }
  }

  //   /**
  //    * @description 查询我创建的圈子
  //    * @author ouyqx-691
  //    * @date 2017-12-11
  //    */
  //   getMyCircle() {
  //     this.communityService.getMyCircle().takeUntil(this.ngUnsubscribe).subscribe(res => {
  //       if (res.result === '0' && res.data) {
  //         let feeds = res.data.list.slice(0, 3);
  //         this.myCircles = feeds;
  //       }
  //     });
  //   };

  //   /**
  //  * @description 查询热门圈子
  //  * @author ouyqx-691
  //  * @date 2017-12-11
  //  */
  //   getHotCircle() {
  //     this.communityService.getHotCircle().takeUntil(this.ngUnsubscribe).subscribe(res => {
  //       if (res.result === '0' && res.data) {
  //         let feeds = res.data.list.slice(0, 3);
  //         this.hotCircles = feeds;
  //       }
  //     });
  //   };

  //   /**
  //  * @description 查询热门圈子
  //  * @author ouyqx-691
  //  * @date 2017-12-11
  //  */
  //  getHotCircle() {
  //     this.communityService.getHotCircle().takeUntil(this.ngUnsubscribe).subscribe(res => {
  //       if (res.result === '0' && res.data) {
  //         let feeds = res.data.list.slice(0, 3);
  //         this.hotCircles = feeds;
  //       }
  //     });
  //   };

  /*
    * @description 查询精选帖子
    * @author ouyqx-691
    * @date 2017-12-12
    *
  */
  getChoiceFeed() {
    this.communityService.getChoiceFeed(this.params).takeUntil(this.ngUnsubscribe).subscribe(res => {
      if (res.result === '0' && res.data) {
        const feeds = res.data.list;
        this.params.page++;
        this.primeFeeds = this.primeFeeds.concat(feeds);
        this.infiniteScroll.complete();

        if (!res.data.hasNextPage) {
          if (feeds.length === 0) {
            this.stateStore.haveFeeds = false;
          }
         this.infiniteScroll.enable(false);
        }
      }
    });
  };

  /**
   * @description 查询圈子所有成员
   * @author ouyqx-691
   * @date 2017-12-14
   */
  getCommunityIndexInfo() {
    this.communityService.getCommunityIndexInfo().takeUntil(this.ngUnsubscribe).subscribe(res => {
      if (res.result === '0' && res.data) {

        this.banners = res.data.advertisementList;
        // 我的圈子
        if (res.data.myCircleList) {
          const myCircles = res.data.myCircleList;
          if (myCircles.length > 4) {
            this.myCircles = res.data.myCircleList.slice(0, 4);
          } else {
            this.myCircles = myCircles;
          }
        }
        // 热门圈子
        if (res.data.hotCircleList) {
          const hotCircles = res.data.hotCircleList;
          if (hotCircles.length > 4) {
            this.hotCircles = res.data.hotCircleList.slice(0, 4);
          } else {
            this.hotCircles = hotCircles;
          }
        }
        // this.primeFeeds = res.data.feedList;
      }
    });
  };

  /**
   * @description 换一换热门圈子
   * @author ouyqx-691
   * @date 2017-12-15
   */
  changeHotCircle() {
    this.communityService.changeHotCircle().takeUntil(this.ngUnsubscribe).subscribe(res => {
      if (res.result === '0' && res.data) {
        if (res.data.length > 0) {
          this.hotCircles = res.data;
        } else {
          this.stateStore.canChange = false;
          this.tools.showToast('没有更多了');
        }
      }
    });
  }

  ngOnInit() {

    // this.getSlider();
    // this.getMyCircle();
    // this.getHotCircle();
    this.getCommunityIndexInfo();
    this.getChoiceFeed();
  }

  ngOnDestroy() {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
  };
}
